class LanguageTrainingPartner(AITrainingPartner):
    """语言学习AI陪练"""

    def __init__(self, name, target_language="english"):
        super().__init__(name)
        self.target_language = target_language
        self.vocabulary = self._load_vocabulary()
        self.grammar_rules = self._load_grammar_rules()

    def _load_vocabulary(self):
        return {
            "easy": [
                {"word": "hello", "translation": "你好", "sentence": "Hello, how are you?"},
                {"word": "apple", "translation": "苹果", "sentence": "I eat an apple every day."}
            ],
            "medium": [
                {"word": "diligent", "translation": "勤奋的", "sentence": "She is a diligent student."},
                {"word": "ambiguous", "translation": "模糊的", "sentence": "The instructions were ambiguous."}
            ],
            "hard": [
                {"word": "perseverance", "translation": "毅力", "sentence": "Success requires perseverance."},
                {"word": "meticulous", "translation": "一丝不苟的", "sentence": "He is meticulous in his work."}
            ]
        }

    def _load_grammar_rules(self):
        return {
            "simple_present": "用于描述习惯、真理和固定安排",
            "present_continuous": "用于描述正在进行的动作",
            "present_perfect": "用于描述与现在有关的过去动作"
        }

    def generate_exercise(self, exercise_type="vocabulary"):
        """生成语言练习"""
        if exercise_type == "vocabulary":
            return self._generate_vocabulary_exercise()
        elif exercise_type == "grammar":
            return self._generate_grammar_exercise()
        elif exercise_type == "conversation":
            return self._generate_conversation_exercise()

    def _generate_vocabulary_exercise(self):
        level_vocab = self.vocabulary[self.difficulty]
        word_data = random.choice(level_vocab)

        exercise = {
            "type": "vocabulary",
            "question": f"请翻译这个词: '{word_data['word']}'",
            "correct_answer": word_data["translation"],
            "hint": f"例句: {word_data['sentence']}",
            "word": word_data["word"]
        }
        return exercise

    def _generate_grammar_exercise(self):
        sentences = {
            "easy": {
                "sentence": "She ___ to school every day. (go)",
                "correct": "goes",
                "rule": "simple_present"
            },
            "medium": {
                "sentence": "I ___ TV when he called. (watch)",
                "correct": "was watching",
                "rule": "past_continuous"
            },
            "hard": {
                "sentence": "By next year, I ___ here for five years. (work)",
                "correct": "will have been working",
                "rule": "future_perfect_continuous"
            }
        }

        exercise_data = sentences[self.difficulty]
        exercise = {
            "type": "grammar",
            "question": f"填空: {exercise_data['sentence']}",
            "correct_answer": exercise_data["correct"],
            "rule": exercise_data["rule"]
        }
        return exercise

    def _generate_conversation_exercise(self):
        scenarios = {
            "easy": {
                "situation": "在餐厅点餐",
                "starter": "服务员: 'What would you like to order?'",
                "expected_response": "I would like a hamburger and a coke."
            },
            "medium": {
                "situation": "商务会议中表达意见",
                "starter": "同事: 'What do you think about this proposal?'",
                "expected_response": "I think it's a good idea, but we should consider the budget."
            },
            "hard": {
                "situation": "学术讨论",
                "starter": "教授: 'How would you interpret these research findings?'",
                "expected_response": "Based on the data, it appears that the hypothesis is supported, though further research is needed."
            }
        }

        scenario = scenarios[self.difficulty]
        exercise = {
            "type": "conversation",
            "situation": scenario["situation"],
            "starter": scenario["starter"],
            "expected_response": scenario["expected_response"]
        }
        return exercise

    def evaluate_response(self, user_response, correct_answer):
        """评估语言回答"""
        # 简化的评估逻辑，实际中可以更复杂
        user_lower = user_response.lower().strip()
        correct_lower = correct_answer.lower().strip()

        if user_lower == correct_lower:
            return {"score": 1.0, "correct": True, "feedback": "完美！"}
        elif user_lower in correct_lower or correct_lower in user_lower:
            return {"score": 0.7, "correct": True, "feedback": "接近正确，但可以更准确"}
        else:
            return {"score": 0.0, "correct": False, "feedback": "需要再练习一下"}

    def provide_feedback(self, user_response, evaluation):
        """提供详细反馈"""
        base_feedback = evaluation["feedback"]

        if evaluation["score"] < 0.5:
            return f"{base_feedback} 建议多练习这个知识点。"
        elif evaluation["score"] < 0.8:
            return f"{base_feedback} 你已经掌握了大部分内容。"
        else:
            return f"{base_feedback} 做得很好！"